package linuxbox.common.taglib;

import java.io.*;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;

import org.apache.log4j.Logger;

import linuxbox.BoxStatic;
import linuxbox.util.*;
import linuxbox.exception.*;

public class ExceptionTag extends TagSupport {
    protected static Logger logger = Logger.getLogger(ExceptionTag.class.getName());
    static final String NUMBER_FORMAT_ID = "F00012";
    static final String UNKNOWN_ERROR_ID = "F00013";

    public int doStartTag() throws JspException {
        try {
            HttpSession hs = pageContext.getSession();

            if (hs != null) {
              Throwable e = (Throwable)hs.getAttribute(BoxStatic.EXCEPTION);
              if (e == null)
              {
                e = (Throwable)pageContext.getRequest().getAttribute(pageContext.EXCEPTION);
              }
              if (e != null)
              {
                if (logger != null)
                  logger.error(e.getMessage());
                e.printStackTrace();
              }

                StringWriter sw = new StringWriter();
            // convert carriage returns to </br> for html display
                HtmlFilterWriter hw = new HtmlFilterWriter(sw);
                PrintWriter pw = new PrintWriter(hw);

//                Exception e = (Exception)hs.getAttribute(BoxStatic.EXCEPTION);
//                if (e == null)
//                  e = pageContext.getException();

                displayException(e, pw);
                String str = sw.toString();
                try {
                    pageContext.getOut().write(str);
                } catch (IOException ioe) {
                    throw new JspException(ioe.getMessage());
                }
                logger.error(str);

                pw.close();
                hw.close();
                sw.close();

            }
        } catch (Exception exc) {
            logger.error(exc);
        }

        return EVAL_BODY_INCLUDE;
    }

    void displayException(Throwable aException, PrintWriter aPW) {
        if (aException != null) {
            if (aException instanceof LinuxboxException) {
                LinuxboxException ne = (LinuxboxException)aException;
                String displayError = BoxStatic.excHelper.getErrorMessage(ne);
                aPW.write("<b>" + ne.getErrorID() + ":</b>");
                aPW.write("<h4>" + displayError + "</h4>");
            } else {
                aPW.write("<h4>" + aException.getClass().getName() + "</h4>");
            }

            if (BoxStatic.PRINT_STACK_TRACE) {
                aPW.write("<br><br>");
                String message = aException.getMessage();

                if (message != null) {
                    aPW.write("<b>" + message + "<b>");
                }
                aException.printStackTrace(aPW);
            }
        } else {
            aPW.write("<h3>please check jspException</h3>");
        }
    }
}
